

__author__="chris"
__date__ ="$Jul 26, 2009 12:30:51 AM$"

__metaclass__ = type

import Common
from Entities import Entities

#import Interpolator
import pygame
#import sys
#import os
#import random
import pygame.sprite
import math
#import Numeric
from pygame.locals import *




#-----Vector-Class-----

class Vector(object):

      def __init__(self, coord1,coord2):
         self.diff = (coord2[0] - coord1[0], coord2[1] - coord1[1])
         #print self.diff

      def distance(self):
         self.a = self.diff[0]
         self.b = self.diff[1]
         return math.sqrt(self.a**2 + self.b**2)

      def unit(self):
         distance = self.distance()
         self.aunit = self.a/distance
         self.bunit = self.b/distance
         return self.aunit, self.bunit


#-----Tree-Class-----

class Tree(pygame.sprite.Sprite): #loads the tree
   def __init__(self, x, y):
      pygame.sprite.Sprite.__init__(self)
      tree_file_name = "tree.bmp"
      self.image, self.rect = main.load_bitmap(tree_file_name, -1)
      #print tree_file_name
      screen = pygame.display.get_surface()
      self.area = screen.get_rect()
      self.rect.midbottom = x, y
      self.x , self.y = self.rect.midbottom

   def pos(self, x, y):
      self.x = x
      self.y = y

   def move(self):
      newpos = self.rect.move(self.x,self.y)
      self.rect = newpos

   def update(self):
      self.image
      self.move()

#-----Missile-Class-----

class Missile(pygame.sprite.Sprite):
   def __init__(self, x, y, angle):
      pygame.sprite.Sprite.__init__(self)
      sprite_file_name = "bullet.bmp"
      self.image, self.rect = Common.load_bitmap(sprite_file_name, -1)
      screen = pygame.display.get_surface()
      self.area = screen.get_rect()
      self.rect.center = x, y
      self.x , self.y = self.rect.center
      self.angle = angle
      playyaw = pygame.transform.rotate
      self.image = playyaw(self.image, self.angle)
      self.rect = self.image.get_rect()
      self.rect.center = x, y
      
      self.corrector = 0.0174532925
      self.x = x
      self.y = y
      killx, killy = pygame.mouse.get_pos()
      self.xdist = abs(killx - self.x)
      self.ydist = abs(killy - self.y)
      trux = killx - self.x
      truy = killy - self.y
      theta = math.atan2(killy,killx)
      self.distance = math.sqrt((self.xdist^2 + self.ydist^2))
      movx = 0
      movy = 0

      self.speed = 25

      def move(self):
         cx = 0

         dx = math.cos((self.angle)*self.corrector)
         dy = math.sin((self.angle)*self.corrector)
         self.x+=dx
         self.y+=dy
         self.movex = dx
         self.movey = dy
         self.rect = self.x, self.y

      def update(self):
         self.move()

#-----Cursor-----

class cursor(pygame.sprite.Sprite): #loads the tree
   def __init__(self):
      pygame.sprite.Sprite.__init__(self)
      sprite_file_name = "crosshairs.bmp"
      self.image, self.rect = Common.load_bitmap(sprite_file_name, -1)
      screen = pygame.display.get_surface()
      self.area = screen.get_rect()
      self.rect.center = pygame.mouse.get_pos()
      self.x , self.y = self.rect.center

   def update(self):
      self.rect.center = pygame.mouse.get_pos()
      
      
#-----Player-Class-----

class Player(pygame.sprite.Sprite): #draws the player

   def __init__(self, x, y):
      pygame.sprite.Sprite.__init__(self)
      play_file_name = "person.bmp"
      self.image, self.rect = Common.load_bitmap(play_file_name, -1)
      self.newimg = self.image
      #print play_file_name
      screen = pygame.display.get_surface()
      self.area = screen.get_rect()
      self.turndirect = 2
      self.angle = 0
      self.rect.center = x, y
      self.x = x
      self.y = y
      self.scrllh = 3
      self.scrllv = 3
      self.movx = 0
      self.movy = 0
      self.corrector = 0.0174532925
      self.speed = 100
      self.movex = 0
      self.movey = 0

      self.left = False
      self.right = False
      self.fward = False
      self.bward = False
      #self.sspeed = 10
      #self.xx, self.yy = abs(pygame.mouse.get_pos() - self.rect.center)

   def update(self):
      self.spin()

   def shoot(self):
      self.bullet = Missile(400,300, self.angle)
      return self.bullet

   def spin(self):
      xmouse, ymouse = pygame.mouse.get_pos()
      self.xx = abs(pygame.mouse.get_pos()[0] - self.x)
      self.yy = abs(pygame.mouse.get_pos()[1] - self.y)
      self.theta = math.atan2(self.yy,self.xx)

      if xmouse >= self.x and ymouse <= self.y:
            #quadrant I
            rotation = (180*self.theta)/math.pi
      elif xmouse < self.x and ymouse <= self.y:
         #quadrant II
         rotation = 180 - (180*self.theta)/math.pi
      elif xmouse >= self.x and ymouse > self.y:
         #quadrant III
         rotation = 360 - (180*self.theta)/math.pi
      else :
         #quadrant IV
         rotation = 180 + (180*self.theta)/math.pi
      
      self.yaw(rotation)


   def takekey(self, str):
      if str[K_a]:
         self.left = True
      if str[K_d]:
         self.right = True
      if str[K_w]:
         self.fward = True
      if str[K_s]:
         self.bward = True
      self.move()

   def move(self):
      if self.right == True and self.left == False:
         mx = -.1

      elif self.left == True and self.right == False:
         mx = .1

      else:
         mx = 0

      if self.bward == True and self.fward == False:
         my = -.1

      elif self.fward == True and self.bward == False:
         my = .1

      else:
         my = 0
      #strafe movement increment
      dx = math.cos((self.angle+90)*self.corrector)*mx
      dy = math.sin((self.angle+90)*self.corrector)*-mx
      #walk movement increment
      ex = math.cos((self.angle)*self.corrector)*my
      ey = math.sin((self.angle)*self.corrector)*-my

      self.movex = dx+ex
      self.movey = dy+ey

      self.left = False
      self.right = False
      self.fward = False
      self.bward = False

   
   def yaw(self, yaw):
      self.angle = yaw
      if self.angle > 360:
         self.angle = 0
      elif self.angle < 0:
         self.angle = 360
      #print self.angle
      center = self.x, self.y
      playyaw = pygame.transform.rotate
      self.image = playyaw(self.newimg, self.angle)
      self.rect = self.image.get_rect()
      self.rect.center = center

#-----Background-----

class Background(pygame.sprite.Sprite):

   def __init__(self, x, y ):
      pygame.sprite.Sprite.__init__(self)
      background_file_name = "terrain.bmp"
      self.image, self.rect = main.load_bitmap(background_file_name,-1)
      #self.rect = pygame.Rect(400, 300, 800, 600)
      #self.image = pygame.transform.scale(self.image,(x,x),)
      #self.newimg = self.image
      #print background_file_name
      self.screen = pygame.display.get_surface()
      self.area = self.screen.get_rect()
      self.rect.center = (8,-((x-y)/2) + 8)
      self.scrllh = 3
      self.scrllv = 3
      self.movx = 0
      self.movy = 0

   def scrollhori(self, direct):
      self.scrllh = direct
      if self.scrllh == 0:
         self.movx = -1
      if self.scrllh == 1:
         self.movx = 1
      if self.scrllh == 2:
         self.movx = 0

   def scrollvert(self, direct):
      self.scrllv = direct
      if self.scrllv == 0:
         self.movy = -1
      if self.scrllv == 1:
         self.movy = 1
      if self.scrllv == 2:
         self.movy = 0

   def move(self):
      newpos = self.rect.move(self.movx,self.movy)
      self.rect = newpos

   def update(self):
      self.image
      self.move()

#-----Sky-Box-----

class Skybox():
   
   def __init__(self, sky0, sky1, sky2, sky3):
      self.skies = [sky0, sky1, sky2, sky3]
      self.rect = []
      c = 3
      while c >= 0:
         self.rect.append((800, 600))
         c -= 1
      self.screen = pygame.display.get_surface()
      c = len(self.skies)
      self.oldx = 0
      self.newx = 0
      self.angle = 0

   def rotate(self, playangle):
      self.angle = playangle
      self.rect[0] += ((self.angle-self.oldx)/800 , 0)
      self.oldx = self.angle


   def rotatein(self):
      x = (self.oldx - self.newx)
      if self.angle >=1 and self.angle <=90:
         c = 0
         cc = 1
      if self.angle >90 and self.angle <=180:
         c = 1
         cc = 2
      if self.angle >180 and self.angle <= 270:
         c = 2
         cc = 3
      if self.angle >270 and self.angle <=361:
         c = 3
         cc = 0
      self.rect[cc] = (self.rect[c][0]+800, 0)
      newpos = self.rect[c] = (x, 0)
      self.rect[c] = newpos

   def update(self):
      self.rotatein()
      self.screen.blit(self.skies[0][0], self.rect[0])



#-----Main-Class-----

class Main():
   
   #-----Main-Stuff-----

   def __init__(self):

      sres = (800, 600)

      screen = pygame.display.set_mode(sres)
      pygame.display.set_caption('M-7 Engine     Press ESC to quit')
      pygame.mouse.set_visible(0)

      curs = cursor()

      play = Player(400,300)

      bullet = Missile(400, 300, 45)

      ent = Entities(screen, 1024, 1200)
      ent.loadent(play)
      ent.loadent(bullet)
      ent.loadent(curs)

      ent.run()
     

pygame.init()
if __name__ == '__main__':
   global main
   main = Main()